<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH."libraries/AdminController.php");
class Asuransi_tawar extends AdminController {  
	
	var $status_form = "'UNPROSES','UPLOAD','PROSES','ACCEPT','REJECT'";
	var $dis = "";
	var $is_detail = FALSE;
	
	function __construct()   
	{
		parent::__construct();    
		$this->_set_action();
		$this->_set_action(array("detail"),"ITEM");
		$this->cat_search = array(
			'all'						=> 'Pilih Kategory',
			'form_principal_name'		=> 'Nama Principal',
			'form_obligee_name'			=> 'Nama Obligee',
			'form_bank_garansi_type'	=> 'Tipe Bank Garansi'
			/*'form_obligee_address'		=> 'Alamat Obligee',
			'form_principal_address'	=> 'Alamat Principal',
			'form_document_number'		=> 'Nomor Dokumen',
			'form_document_desc'		=> 'Penjelasan Dokumen',
			'marketing_name'			=> 'Nama Marketing',
			'asuransi_name'				=> 'Nama Asuransi',
			'cur_name'					=> 'Nama Mata Uang'*/
		);
		$this->_set_title('Transaksi > Tawaran Formulir dari AA Pialang Kepada '.getAsuransi($this->jCfg['user']['asuransi']));
		$this->_set_cat_search(array("all"	=> $this->cat_search));	
		$this->DATA->table="tm_formulir";
		$this->load->model("mdl_formulir_asuransi","A");
		$this->folder_view = "transaksi/";
		$this->prefix_view = strtolower($this->_getClass());
		
		//$this->output->enable_profiler(true);

	}

	function index(){
		
		$this->jCfg['isearch']['status'] = "all";
		$this->_releaseSession();
		
		//$this->_set_desc_title('Daftar Penawaran Formulir Kepada Asuransi');	
		
		$mst=isset($this->jCfg['isearch']['status'])?$this->jCfg['isearch']['status']:"all";
		$stat = ($mst=="all")?$this->status_form:"'".$mst."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
				"limit" 	=> $this->per_page, 
				"asuransi"	=> $this->jCfg['user']['asuransi'],
				"status"	=> $stat,
				"start_date"=> $this->jCfg['isearch']['start_date'],
				"end_date"	=> $this->jCfg['isearch']['end_date'],
				"offset" 	=> $this->uri->segment($this->uri_segment)
		) );
		
		$data = $this->_data(array(
				'base_url'		=> $this->own_link.'index'
			)
		);	
					
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function get_note($form_id=""){
		$asuransi_id = $this->jCfg['user']['asuransi'];
		
		$this->output->enable_profiler(FALSE);
		if(trim($form_id) !="" && trim($asuransi_id) != ""){
			$this->DATA->table = "tm_pialang_note";
			$m = $this->db->query("
				select * from tm_pialang_note where note_form_id = '".dbClean($form_id)."'
				AND note_as_id = '".dbClean($asuransi_id)."'
				order by note_id DESC
			")->row();
			if($this->jCfg['user']['id'] == $m->note_receiver){
				$this->DATA->_update(array(
					"note_form_id"		=> dbClean($form_id),
					"note_as_id"		=> dbClean($asuransi_id)
				),array(
					"note_read"	=> 1
				));
			}
			$mm = get_note(array(
				  	"form_id"		=> dbClean($form_id),
					"asuransi_id"	=> dbClean($asuransi_id)
				  ));
			$this->_v("note",array('data'=>$mm),FALSE);
		}else{
			echo "empty";
		}
	}
	
	function search(){
		//$this->_set_desc_title('Daftar Penawaran Formulir Kepada Asuransi');	
		$this->load->helper('url');		
		$this->uri_segment = 6;
			
		$column = ''; $keyword = '';				
		if( isset($_POST['column']) && isset($_POST['keyword']) ){
			$column = $_POST['column'];
			$keyword = url_title($_POST['keyword']);
			$this->setiSearch(array(
				"start_date"=> dbClean($_POST['dstart']),
				"end_date"	=> dbClean($_POST['dend']),
				"status"	=> dbClean($_POST['status']),
				"per_page"	=> dbClean($_POST['per_page'])
			));
		}else{
			$column =  $this->uri->segment(4);
			$keyword =   $this->uri->segment(5);
		}	
		
		$stat = ($this->jCfg['isearch']['status']=="all")?$this->status_form:"'".$this->jCfg['isearch']['status']."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
								"limit" 	=> $this->per_page, 
								"offset" 	=> $this->uri->segment($this->uri_segment),
								"column"	=> $column,
								"asuransi"	=> $this->jCfg['user']['asuransi'],
								"start_date"=> $this->jCfg['isearch']['start_date'],
								"end_date"	=> $this->jCfg['isearch']['end_date'],
								"status"	=> $stat,
								"keyword"	=> (trim($keyword)=="all")?"":$keyword
							) );
		$k=($keyword=="")?"all":$keyword;
		$data = $this->_data(
			array(
				'base_url'		=>$this->own_link.'search/'.$column.'/'.$k
			)
		);	
		
		
		$this->header_page['key']=pTxt(($k=="all")?"":$k);
		$this->header_page['col']=$column;	
		$this->header_page['cat_search'] = array($column	=>	$this->cat_search);	
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
		
	function edit($id=''){
		$id=dbClean(trim($id));
		//$this->_set_desc_title('Update Data Penawaran Formulir Kepada Asuransi');	
		
		if(trim($id)!=''){
			$this->data_form = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
			
			$this->DATA->table = "tm_asuransi_form";
			$vf = $this->DATA->data_id(array(
					'as_form_id'	=> $id,
					'as_asuransi_id'=> $this->jCfg['user']['asuransi']
				));
										
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"val2"		=> $vf,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}	
	
	function detail($id=''){
		$this->is_detail = TRUE;
		$this->dis = 'disabled="disabled"';
		
		$id=dbClean(trim($id));		
		if(trim($id)!=''){
			$this->data_form = $this->DATA->data_id(array(
					'form_id'	=> $id
				));
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
			
			$this->DATA->table = "tm_asuransi_form";
			$vf = $this->DATA->data_id(array(
					'as_form_id'	=> $id,
					'as_asuransi_id'=> $this->jCfg['user']['asuransi']
				));
										
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"val2"		=> $vf,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}	
	
	function save(){	
		
		$did = $this->DATA->data_id(array(
			'form_id' => dbClean($_POST['form_id'])
		));
		
		// insert detail ....
		$das = array(
			"as_form_id"		=> dbClean($_POST['form_id']),
			"as_asuransi_id"	=> dbClean($this->jCfg['user']['asuransi']),
			"as_jasa_penjamin"	=> dbClean($_POST['as_jasa_penjamin']),
			"as_biaya_adm"		=> dbClean($_POST['as_biaya_adm']),
			"as_bea_materai"	=> dbClean($_POST['as_bea_materai']),
			"as_hak_asuransi"	=> dbClean($_POST['as_hak_asuransi']),
			"as_hak_bank"		=> dbClean($_POST['as_hak_bank']),
			"as_status"			=> dbClean($_POST['form_status'])
		);
		$this->DATA->table = "tm_asuransi_form";
		$this->_save_master( 
			$das,
			array(
				'as_form_id' 		=> dbClean($_POST['form_id']),
				'as_asuransi_id'	=> dbClean($this->jCfg['user']['asuransi']),
			),0			
		);
		
		// insert note.....
		if(trim(dbClean($_POST['note'])) != ""){
			$dnote = array(
				"note_form_id"		=> dbClean($_POST['form_id']),
				"note_as_id"		=> dbClean($this->jCfg['user']['asuransi']),
				"note_sender"		=> $this->jCfg['user']['id'],
				"note_receiver"		=> $did->user_add,
				"note_content"		=> dbClean($_POST['note']),
				"note_read"			=> 0,
				"note_date"			=> date("Y-m-d H:i:s")
			);
			$this->DATA->table = "tm_pialang_note";
			$this->DATA->_add($dnote);
		}
		
		// update form
		$data = array(
			'form_status' 				=> 'PROSES'
		);
		
		$this->DATA->table = "tm_formulir";
		$a = $this->_save_master( 
			$data,
			array(
				'form_id' => dbClean($_POST['form_id'])
			),
			dbClean($_POST['form_id'])			
		);
		
		// send email...
		if(cfg('online')==1){
			$email_asuransi = get_email_asuransi($this->jCfg['user']['asuransi']);
			$msg = "Asuransi ".getAsuransi($this->jCfg['user']['asuransi'])." Telah Mereply Penawaran Asuransi ";
			$msg .= "\n pada tanggal : ".date("Y-m-D H:i:s");
			$msg .= "\n Silahkan buka website untuk melihat penawaran tersebut";
			if(trim(dbClean($_POST['note'])) != ""){
				$msg .= "Note : ".dbClean($_POST['note']);
			}
			
			$this->sendEmail(array(
				"type"		=> "text",
				"to"		=> getCfgApp('email_admin'),
				"bcc"		=> getCfgApp('email_bcc'),
				"from"		=> getCfgApp('email_noreply'),
				"subject"	=> "Balasan Penawaran Formulir Dari ".getAsuransi($this->jCfg['user']['asuransi']),
				"title"		=> "Balasan Penawaran Formulir Dari ".getAsuransi($this->jCfg['user']['asuransi']),
				"message"	=> $msg
			));
		}

		redirect($this->own_link);
	}
	
}
